<?php

/**

 */
require_once dirname(__FILE__) . '/Raven/Autoloader.php';

class Raven_Lite extends PhalApi_Response {

    protected $Raven;
    protected $callback = '';


    public function __construct($SentryDSN = null, $callback = null) {

        if ($callback) {
            $this->callback = $this->clearXss($callback);
            $this->addHeaders('Content-Type', 'text/javascript; charset=utf-8');
        }
        if (!$SentryDSN) {
            $SentryDSN = DI()->config->get('app.SentryDSN');
        }

        Raven_Autoloader::register();
        //SentryDSN
//        $this->Raven = new Raven_Client('http://andy:HFdfDfgTmVEQd24N@sentry.menma.me/2');
        $this->Raven = new Raven_Client($SentryDSN);

        $error_handler = new Raven_ErrorHandler($this->Raven);
        $error_handler->registerExceptionHandler();
        $error_handler->registerErrorHandler();
        $error_handler->registerShutdownFunction();

    }

    /**
     * 对回调函数进行跨站清除处理
     *
     * - 可使用白名单或者黑名单方式处理，由接口开发再实现
     */
    protected function clearXss($callback) {
        return $callback;
    }

    protected function formatResult($result) {
        if ($result['ret'] != 200) {
//            $this->Raven->captureException($result['msg']);
            $this->Raven->captureMessage($result['msg'],'',$result['ret']>1000?'info':'error');
        }
        $result = $this->callback ? $this->callback . '(' . json_encode($result) . ')' : json_encode($result);
        echo $result;
    }
}


